{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "44a76571",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "2.5\n",
      "10\n"
     ]
    }
   ],
   "source": [
    "import tensorflow as tf\n",
    "\n",
    "tf.compat.v1.disable_eager_execution() # need to disable eager in TF2.x\n",
    "# Build a graph.\n",
    "data1 = tf.constant(2.5)\n",
    "data2 = tf.Variable(10,name='var')\n",
    "\n",
    "# Launch the graph in a session.\n",
    "sess = tf.compat.v1.Session()\n",
    "\n",
    "# Evaluate the tensor `c`.\n",
    "print(sess.run(data1)) # prints 30.0\n",
    "init = tf.compat.v1.global_variables_initializer()\n",
    "sess.run(init)\n",
    "print(sess.run(data2))\n",
    "sess.close()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "cf1ec231",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "2.5\n",
      "10\n"
     ]
    }
   ],
   "source": [
    "import tensorflow as tf\n",
    "\n",
    "tf.compat.v1.disable_eager_execution() # need to disable eager in TF2.x\n",
    "# Build a graph.\n",
    "data1 = tf.constant(2.5)\n",
    "data2 = tf.Variable(10,name='var')\n",
    "\n",
    "# Launch the graph in a session.L\n",
    "sess = tf.compat.v1.Session()\n",
    "init = tf.compat.v1.global_variables_initializer()\n",
    "with sess:\n",
    "    sess.run(init)\n",
    "    # Evaluate the tensor `c`.\n",
    "    print(sess.run(data1)) # prints 30.0\n",
    "    print(sess.run(data2))"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "6787d16a",
   "metadata": {},
   "source": [
    "四则运算"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "id": "ed99d5a6",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "8\n",
      "8\n",
      "48\n",
      "-2\n",
      "0.75\n"
     ]
    }
   ],
   "source": [
    "import tensorflow as tf\n",
    "tf.compat.v1.disable_eager_execution()\n",
    "data1 = tf.constant(6)\n",
    "data2 = tf.Variable(2,name='var')\n",
    "dataAdd = tf.add(data1,data2)\n",
    "dataCopy = tf.compat.v1.assign(data2,dataAdd) #dataAdd->data2\n",
    "dataMul = tf.multiply(data1,data2)\n",
    "dataSub = tf.subtract(data1,data2)\n",
    "dataDiv = tf.divide(data1,data2)\n",
    "init = tf.compat.v1.global_variables_initializer()\n",
    "with tf.compat.v1.Session() as sess:\n",
    "    sess.run(init)\n",
    "    print(sess.run(dataAdd))\n",
    "    print(sess.run(dataCopy))\n",
    "    print(sess.run(dataMul))\n",
    "    print(sess.run(dataSub))\n",
    "    print(sess.run(dataDiv))\n",
    "    "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "id": "fbd76062",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "8.0\n",
      "end!\n"
     ]
    }
   ],
   "source": [
    "#placehold\n",
    "import tensorflow as tf\n",
    "\n",
    "data1 = tf.compat.v1.placeholder(tf.float32)\n",
    "data2  = tf.compat.v1.placeholder(tf.float32)\n",
    "dataAdd = tf.add(data1,data2)\n",
    "with tf.compat.v1.Session() as sess:\n",
    "    print(sess.run(dataAdd,feed_dict={data1:6,data2:2}))\n",
    "print(\"end!\")"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.8"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
